# 逗你玩-精简版本

from tkinter import *
from random import *

def callback(event):
    global r
    if(event.widget == blist[r]):
        a = r    #暂存上次随机数
        vlist[r].set("NULL")
        while( r == a):
            r = randint(0,8)
        r = randint(0, 8)
        vlist[r].set("逗你玩")

root = Tk()

vlist = [] # 存放按钮文本信息
blist = [] # 存放按钮

# 利用循环产生按钮文本列表
for i in range(9):
    var = StringVar()
    var.set("NULL")
    vlist.append(var)

# 利用循环产生按钮列表
j = -1
for i in range(9):
    if ( i % 3 == 0):
        j = j+1
    b = Button(root, height = 3, width = 10, textvariable = vlist[i])
    b.bind("<Enter>", callback)
    # < Button - 1 >
    # < Button - 2 >
    # < Button - 3 >
    b.grid(row = j, column = i % 3)
    blist.append(b)

r = randint(0,8)
vlist[r].set("逗你玩")

mainloop()